﻿using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45Tool.Service;
using nblTool.utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;
using WindowsFormsApp1.Utils;

namespace Jn698._45GWTool.ViewModels
{
    public class ModeBitViewModel:ViewModelBase
    {
        private BitStringItemService bitStringItemService = new BitStringItemService();
        public static GWCmdMakeUp GWCmdMakeUp;

        private string title;

        public string Title
        {
            get { return title; }
            set { title = value; }
        }
        private ObservableCollection<ModeBitData> modeBitDataList;

        public ObservableCollection<ModeBitData> ModeBitDataList
        {
            get { return modeBitDataList; }
            set { modeBitDataList = value; }
        }

        public ModeBitViewModel() 
        {
            GWCmdMakeUp = ServiceLocator.Current.GetInstance<GWCmdMakeUp>();
            if (GWCmdMakeUp.SubItemName == null) 
            {
                return;
            }
            Title= GWCmdMakeUp.SubItemName;
            var items = bitStringItemService.SelectByName(GWCmdMakeUp.SubItemName);
            byte[] byteData = StringUtils.strToHexByte(GWCmdMakeUp.DataValue);
            Array.Reverse(byteData);
            List<bool> value = new List<bool>();
            foreach (var item in byteData)
            {
                value.AddRange(ByteUtils.GetBitBool(item));
            }
            value.Reverse();
            int index = 0;
            List<ModeBitData> list = new List<ModeBitData>();
            foreach (var item in items)
            {
                ModeBitData data = new ModeBitData();
                data.BNo = (int)item.BNo;
                data.BDes = item.BDes;
                data.BVlue = value[index++] ? 1 : 0;
                data.BText= item.BText;
                list.Add(data);
            }
            ModeBitDataList = new ObservableCollection<ModeBitData>(list);
        }

        public RelayCommand<Window> BtnOKCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) => {
                    List<bool> value = new List<bool>();
                    for (int i = 0; i < modeBitDataList.Count; i++)
                    {
                        value.Add(modeBitDataList[i].BVlue != 0 ? true : false);
                    }
                    
                    value.Reverse();
                    var d = ByteUtils.bytesToHexString(ByteUtils.GetByteArrary(value));

                    GWCmdMakeUp.DataValue = d;
                    w.DialogResult = true;
                });
                return command;
            }
        }

        public RelayCommand<Window> BtnCancelCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) => {
                    w.DialogResult = false;
                });
                return command;
            }
        }
    }
}
